import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ToolUtil } from '@/utils/tool.util';
import { UserModule } from './modules/user/user.module';
import { BlogModule } from './modules/blog/blog.module';
import { WebHookModule } from './modules/web-hook/web-hook.module';
import { WechatModule } from './modules/wechat/wechat.module';
import { TypeOrmModule } from '@nestjs/typeorm';
import { RoleModule } from './modules/role/role.module';
import { MockModule } from './modules/mock/mock.module';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { AuthModule } from './modules/auth/auth.module';
import { PermissionModule } from './modules/permission/permission.module';

@Module({
  imports: [
    ConfigModule.forRoot({
      //全局使用，无需在其他模块中导入它
      isGlobal: true,
      // 自定义 env 文件路径，默认情况下，程序在应用程序的根目录中查找.env文件
      envFilePath: ['.env', '.env.test'],
    }),
    // 异步配置
    TypeOrmModule.forRootAsync({
      inject: [ConfigService],
      useFactory: async (configService: ConfigService) => {
        return {
          type: 'mysql',
          host: configService.get('DB_HOST'),
          port: configService.get('DB_PORT'),
          username: configService.get('DB_USER'),
          password: configService.get('DB_PASSWORD'),
          database: configService.get('DB_DATABASE'),
          timezone: '+08:00',
          charset: 'utf8mb4',
          synchronize: true, // 是否自动同步载入的模型，会直接修改数据库，仅开发时使用防止删除数据库
          autoLoadEntities: true, // 模型是否自动加载
        };
      },
    }),
    UserModule,
    BlogModule,
    WebHookModule,
    WechatModule,
    RoleModule,
    PermissionModule,
    MockModule,
    AuthModule,
  ],
  controllers: [AppController],
  providers: [AppService, ToolUtil],
})
export class AppModule {}
